﻿using System;
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Ycjw.PipeLine
{
    public partial class SplashForm : Form
    {
        public bool CloseIt { get; set; }
        public static void Create()
        {
            m_Instance = new SplashForm();
        }
        static SplashForm m_Instance;
        public static SplashForm Instance
        {
            get
            {
                if(m_Instance == null)
                {
                    m_Instance = new SplashForm();
                }
                return m_Instance;
            }
        }
        SplashForm()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            
            this.TopLevel = true;
            this.BackgroundImage = Image.FromFile(Commons.Paths.ApplicationResourcesDir + "\\三维管线.png");
            this.BackgroundImageLayout = ImageLayout.Stretch;
            this.pictureBoxLogo.BackgroundImage = Image.FromFile(Commons.Paths.ApplicationResourcesDir + "\\logo.png");
        }
        private void WaitClose()
        {
            Task.Factory.StartNew(new Action(()=> {
                while (!CloseIt)
                {
                    Thread.Sleep(200);
                }
                this.Invoke(new System.Action(() =>
                {
                    this.Close();
                }));
            }));
        }

        public void SetMessage(string msg)
        {
            this.Invoke(new System.Action(() =>
            {
                this.uiLabelMessage.Text = msg;
            }));
        }

        protected override void OnLoad(EventArgs e)
        {
            SetMessage("正在启动，请稍等……");
            base.OnLoad(e);
            WaitClose();
        }
    }
}
